package com.jinzhi.jzweb.controller.moduleConfig;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jinzhi.common.utils.Result;
import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.pojo.ModuleConfig.LotteryManagementDto;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.jzweb.service.moduleConfig.LotteryManagementService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;


/*
 *  @author : JingChangTian
 *  @date   : 2023/9/28 9:52
 *  @description :
 *
 */
@Controller
@RequestMapping("/lottery")
@Api(value = "抽签管理",tags = "抽签管理")
public class LotteryManagementController {


    private final LotteryManagementService lotteryManagementService;


    private final StuSignUpComService stuSignUpComService;

    public LotteryManagementController(LotteryManagementService lotteryManagementService, StuSignUpComService stuSignUpComService) {
        this.lotteryManagementService = lotteryManagementService;
        this.stuSignUpComService = stuSignUpComService;
    }

    @ApiOperation(value = "考生信息导入，匹配赛场", notes = "考生信息导入，匹配赛场")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @PostMapping(value = "/lotteryBinding")
    @ResponseBody
    public Result<Object> lotteryBinding(@RequestParam("file") MultipartFile file,@RequestParam Long cbatchId){
        return lotteryManagementService.lotteryBinding(file,cbatchId);
    }

    @ApiOperation(value = "赛道绑定页面，查询所有信息", notes = "赛道绑定页面，查询所有信息")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @PostMapping(value = "/getLotterySign")
    @ResponseBody
    public Result<Object> getLotterySign(@RequestParam Integer current ,@RequestParam Integer number ){
        return lotteryManagementService.getLotterySign(current , number);
    }


    @ApiOperation(value = "删除赛道绑定关系", notes = "删除赛道绑定关系")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @PostMapping(value = "/updateSign")
    @ResponseBody
    public Result<Object> updateSign(@RequestParam Long cbatchId){
        return lotteryManagementService.updateSign(cbatchId);
    }



    @ApiOperation(value = "赛道抽签列表页面", notes = "赛道抽签列表页面")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/draw")
    //@RequiresPermissions("jzweb:draw:draw")
    String draw() {
        return "jzweb/draw/draw";
    }


    @ApiOperation(value = "赛道表新增", notes = "赛道表新增")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/add")
    //@RequiresPermissions("jzweb:track:trackchart")
    String add() {
        return "jzweb/track/add";
    }

    @ApiOperation(value = "赛道修改", notes = "赛道表修改")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/edit")
        //@RequiresPermissions("jzweb:track:trackchart")
    String edit() {
        return "jzweb/track/edit";
    }


    @ApiOperation(value = "赛道表列表", notes = "赛道表列表")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/trackchart")
        //@RequiresPermissions("jzweb:track:trackchart")
    String trackchart() {
        return "jzweb/track/trackchart";
    }


    @ApiOperation(value = "赛道表详情列表", notes = "赛道表详情列表")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/info")
        //@RequiresPermissions("jzweb:track:trackchart")
    String info() {
        return "jzweb/track/info";
    }



    /**
     * 导如模板 信息
     * @throws Exception
     */
    @ApiOperation(value = "赛道绑定导入模版", notes = "赛道绑定导入模版")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @GetMapping("/exportList")
    @ResponseBody
    public void exportlist(HttpServletRequest request, HttpServletResponse respons) throws Exception{

        List<LotteryManagementDto> list = new LinkedList<>();
        //导出操作
        EasyPoiUtil.exportExcel(list, "赛道绑定导入模版", "赛道绑定导入模版", LotteryManagementDto.class, "赛道绑定导入模版", respons);

    }



    @ApiOperation(value = "抽签改变状态", notes = "抽签改变状态")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @PostMapping(value = "/updateStu")
    @ResponseBody
    public Result<Object> updateStu(@RequestParam Long cbatchId){
        StuSignUpComDO sign = new StuSignUpComDO();
        sign.setLots(1);
        return  Result.ok(stuSignUpComService.update(sign , new EntityWrapper<StuSignUpComDO>().eq("cbatch_id",cbatchId)));
    }


    @ApiOperation(value = "修改加密", notes = "修改加密")
    @ApiImplicitParams({
    })
    @ApiResponses({@ApiResponse(code = 0, message = "操作成功"),@ApiResponse(code = 500, message = "服务器内部异常")})
    @PostMapping(value = "/updateNum")
    @ResponseBody
    public Result<Object> trtr(@RequestParam Long cbatchId){
       List<StuSignUpComDO> list =  stuSignUpComService.selectList(new EntityWrapper<StuSignUpComDO>());
       Random random = new Random();
       list.forEach(a ->{
            a.setRandomCipher(String.valueOf(random.nextInt(900000)+100000));
       });
       List<StuSignUpComDO> list1 = list.stream().filter(a -> a.getCompetitionType().equals("1")).collect(Collectors.toList());
       List<StuSignUpComDO> list2 = list.stream().filter(a -> a.getCompetitionType().equals("2")).collect(Collectors.toList());
       List<StuSignUpComDO> list3 = list.stream().filter(a -> a.getCompetitionType().equals("3")).collect(Collectors.toList());
       List<StuSignUpComDO> list4 = list.stream().filter(a -> a.getCompetitionType().equals("4")).collect(Collectors.toList());
       list.clear();

       //教师组
        if(!list1.isEmpty()){
            for (int i = 0; i < list1.size() ; i++) {
                list1.get(i).setNum1("J-0"+(i+1)+"A");
                if(i>8){
                    list1.get(i).setNum2((i+1)+"AJ");
                }else {
                    list1.get(i).setNum2("0"+(i+1)+"AJ");
                }
                list.add(list1.get(i));
            }

        }


        //中职组
        if(!list2.isEmpty()){
            for (int j = 0; j < list2.size() ; j++) {
                list2.get(j).setNum1("Z-0"+(j+1)+"A");
                if(j>8){
                    list2.get(j).setNum2((j+1)+"AZ");
                }else {
                    list2.get(j).setNum2("0"+(j+1)+"AZ");
                }
                list.add(list2.get(j));
            }
        }

        //高校组
        if(!list3.isEmpty()){
            for (int k = 0; k < list3.size() ; k++) {
                list3.get(k).setNum1("G-0"+(k+1)+"A");

                if(k>8){
                    list3.get(k).setNum2((k+1)+"AG");
                }else {
                    list3.get(k).setNum2("0"+(k+1)+"AG");
                }
                list.add(list3.get(k));
            }

        }

        //国际组
        if(!list4.isEmpty()){
            for (int k = 0; k < list4.size() ; k++) {
                list4.get(k).setNum1("I-0"+(k+1)+"A");

                if(k>8){
                    list4.get(k).setNum2((k+1)+"AI");
                }else {
                    list4.get(k).setNum2("0"+(k+1)+"AI");
                }
                list.add(list4.get(k));
            }

        }

        stuSignUpComService.updateBatchById(list);
        return Result.ok();
    }

}
